<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="课程包名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入课程包名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['school:schoolinfo:edit']">添加课程</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['school:schoolinfo:edit']">批量移除课程</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="课程包名称" align="center" prop="name" />
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['school:schoolinfo:edit']">移除课程</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  <SelectCourse ref="select" :id="queryParams.includeSchoolId" @ok="handleQuery" />
  </div>
</template>

<script>
  import {
    listCourse
  } from "@/api/bus_web/course/course";
  import {
    manCourse
  } from "@/api/bus_web/school/schoolinfo";
import SelectCourse from "./selectCourse";
  export default {
    name: "HandleCourse",
    components: {
      SelectCourse
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        options: [],
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 课程包管理表格数据
        courseList: [],
        // 查询参数
        queryParams: {
         pageNum: 1,
         pageSize: 10,
         name: null,
         state: 0,
        }
      };
    },
    created() {
        const id = this.$route.params && this.$route.params.id;
        if (id) {
              this.queryParams.includeSchoolId = id;
              this.getList();
          }
    },
    methods: {
      /** 查询课程信息列表 */
      getList() {
        this.loading = true;
        const id = this.$route.params && this.$route.params.id;
        this.queryParams.includeSchoolId = id;
        listCourse(this.queryParams).then(response => {
          this.courseList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.reset();
      },
      // 表单重置
      reset() {
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      handleDelete(row){
        const ids = row.id || this.ids;
        var _courseIdList = new Array()
        if(!Array.isArray(ids)){
          _courseIdList.push(ids)
        }else{
          _courseIdList=ids
        }
        const _data={ id: this.queryParams.includeSchoolId, op:2,courseIdList: _courseIdList }
        this.$modal.confirm('是否确认删除学校课程编号为"' + ids + '"的数据项？').then(function() {
          return manCourse(_data);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.$refs.select.show();
      },
    }
  };
</script>
<style scoped>
  .uploadImage {
    width: 100%;
    height: 100%;
  }
</style>
